package com.mofofa.maozy.components.dialog

import android.annotation.SuppressLint
import android.content.res.Resources
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseDialogAction
import com.mofofa.maozy.databinding.DialogMaozyDatePickerBinding

class DatePickerDialog : DialogFragment(), BaseDialogAction {

    companion object {
        fun showMaozyDialog(
            fragmentManager: FragmentManager,
            dateMode: Int = R.integer.ActionModeDatePickerYYMMDD,
            listener: (String, String) -> Unit,
        ) {
            val dialog = DatePickerDialog()
            dialog.mMaozyDateMaozyMode = dateMode
            dialog.mMaozyOnMaozyActionMaozyListener = listener
            dialog.showNow(fragmentManager, DatePickerDialog::class.java.simpleName)
        }
    }

    private lateinit var mMaozyViewMaozyBinding: DialogMaozyDatePickerBinding
    var mMaozyOnMaozyActionMaozyListener: (String, String) -> Unit = { tag, date -> }
    var mMaozyDateMaozyMode: Int = R.integer.ActionModeDatePickerYYMMDD
    var mMaozyUIMaozyTag: String = ""

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);

        mMaozyViewMaozyBinding = DialogMaozyDatePickerBinding.inflate(layoutInflater)
        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }
    }

    override fun initMaozySubviews() {
        when (this.mMaozyDateMaozyMode) {
            R.integer.ActionModeDatePickerYYMM -> {
                hideMaozyDay()
            }
            R.integer.ActionModeDatePickerMMDD -> {
                hideMaozyYear()
            }
            R.integer.ActionModeDatePickerYY -> {
                hideMaozyMonthMaozyAndMaozyDay()
            }
            else -> {
            }
        }

        mMaozyViewMaozyBinding.submitDate.setOnClickListener {
            val year: Int = mMaozyViewMaozyBinding.datePicker.year
            val month: Int = mMaozyViewMaozyBinding.datePicker.month + 1
            val day: Int = mMaozyViewMaozyBinding.datePicker.dayOfMonth
            val monthStr = if (month < 10) "0$month" else month.toString()
            val dayStr = if (day < 10) "0$day" else day.toString()

            when (this.mMaozyDateMaozyMode) {
                R.integer.ActionModeDatePickerYYMMDD -> {
                    mMaozyOnMaozyActionMaozyListener.invoke(mMaozyUIMaozyTag, "$year-$monthStr-$dayStr")
                }
                R.integer.ActionModeDatePickerYYMM -> {
                    mMaozyOnMaozyActionMaozyListener.invoke(mMaozyUIMaozyTag, "$year-$monthStr")
                }
                R.integer.ActionModeDatePickerMMDD -> {
                    mMaozyOnMaozyActionMaozyListener.invoke(mMaozyUIMaozyTag, "$monthStr-$dayStr")
                }
                else -> {
                    mMaozyOnMaozyActionMaozyListener.invoke(mMaozyUIMaozyTag, "$year-$monthStr-$dayStr")
                }
            }
            dismiss()
        }
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {

    }

    override fun showMaozyWithMaozyUIMaozyTag(gTag: String, fragmentManager: FragmentManager) {
        mMaozyUIMaozyTag = gTag
        showNow(
            fragmentManager,
            DatePickerDialog::class.java.simpleName
        )
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMaozyMonthMaozyAndMaozyDay() {
        try {
            val monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) {
                val monthSpinner: View? = mMaozyViewMaozyBinding.datePicker.findViewById(monthSpinnerId)
                monthSpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }

        try {
            val daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                val daySpinner: View? = mMaozyViewMaozyBinding.datePicker.findViewById(daySpinnerId)
                daySpinner?.visibility = View.GONE
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMaozyYear() {
        try {
            val yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
            if (yearSpinnerId != 0) {
                val yearSpinner: View? = mMaozyViewMaozyBinding.datePicker.findViewById(yearSpinnerId)
                yearSpinner?.setVisibility(View.GONE)
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMaozyMonth() {
        try {
            val monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) {
                val monthSpinner: View? = mMaozyViewMaozyBinding.datePicker.findViewById(monthSpinnerId)
                monthSpinner?.setVisibility(View.GONE)
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    @SuppressLint("DiscouragedApi")
    private fun hideMaozyDay() {
        try {
            val daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
            if (daySpinnerId != 0) {
                val daySpinner: View? = mMaozyViewMaozyBinding.datePicker.findViewById(daySpinnerId)
                daySpinner?.setVisibility(View.GONE)
            }
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }
}